#include "Switch.h"

Switch::Switch()
{
}

Switch &Switch::setCallback(SWITCH_CALLBACK_SIGNATURE)
{
    this->callback = callback;
    return *this;
}

void Switch::setup()
{
    pinMode(SWITCH_ACTION_PIN, OUTPUT); // 设置引脚输出
    digitalWrite(SWITCH_ACTION_PIN, LOW); // 默认输出低电平
    status = SWITCH_CLOSE;
}

void Switch::open() {
    digitalWrite(SWITCH_ACTION_PIN, LOW);
    status = SWITCH_OPEN;
    callback(SWITCH_OPEN);
}

void Switch::close() {
    digitalWrite(SWITCH_ACTION_PIN, HIGH);
    status = SWITCH_CLOSE;
    callback(SWITCH_CLOSE);
}

int Switch::getValue() {
    return status;
}

void Switch::loop()
{
    // 10ms计数一次
    // long now = millis();
    // if (now - last > 10)
    // {
    //     last = now;
    //     // 读取引脚
    //     int value = digitalRead(SWITCH_ACTION_PIN);
    //     if (value != status)
    //     {
    //         status = value;
    //         callback(getValue());
    //         return;
    //     }
    // }
    // 定时上传 3s
    // if (now - upload_last > 5000) {
    //     upload_last = now;
    //     callback(getValue());
    // }
}


